<template>
	<view class="gift-page">
		<view class="swiperlistbox">
			<uni-swiper-dot :info="swiperdata" :current="current" field="content">
				<swiper class="swiper-box" @change="change">
					<swiper-item v-for="(item ,index) in swiperdata" :key="index">
						<image class="image" :src="item.img_url" mode=""></image>
					</swiper-item>
				</swiper>
			</uni-swiper-dot>
		</view>
		<view class="time-box">
			<text class="left-text">限时特惠 先下单先得~</text>
			<view class="time-content">
				<text class="name">距离结束还剩</text>
				<van-count-down
				  use-slot
				  :time="time"
				  @change="onChange"
				>
				  <text class="item">{{ timeData.hours }}</text>
				  <text class="item">{{ timeData.minutes }}</text>
				  <text class="item">{{ timeData.seconds }}</text>
				</van-count-down>
			</view>
		</view>
		<gift-list :giftData="giftList"></gift-list>
		<!-- #ifdef MP-WEIXIN -->
		<van-toast id="van-toast" />
		<!-- #endif -->
	</view>
</template>
<script>
	import giftList from "./components/gift_list.vue"
	// #ifdef MP-WEIXIN
	import Toast from "@/wxcomponents/vant-weapp/toast/toast.js";
	// #endif
	import uniSwiperDot from "@/components/public/uni-swiper-dot";
	import { getDatas , getproductData } from "@/api/getdata.js";
	export default {
		name:"gift",
		components:{
			"gift-list":giftList,
			"uni-swiper-dot":uniSwiperDot,
		},
		data(){
			return {
				current:0,
				swiperdata:[],
				time: 30 * 60 * 60 * 1000,
				timeData: {},
				giftList:[],
			}
		},
		onLoad() {
			this._initData();
			this.$setTopBar({
				title:"值得买",
			});
		},
		methods:{
			async _initData(){
				try{
					uni.showLoading({title:"加载中"});
					let _channel = this.$store.state.channel; 
					let data = await getDatas({channel:_channel});
					let _data = await getproductData({prod_name:"all",channel:_channel})
					this.swiperdata = data.rotation_chart;
					this.setGift(_data);
					uni.hideLoading();
				}catch(e){
					uni.hideLoading();
					// #ifdef MP-WEIXIN
					Toast.fail(e.errMsg);
					// #endif
				}
			},
			change(e){
				this.current = e.detail.current;
			},
			onChange(e) {
			    this.timeData = e.detail;
			},
			setGift(data){
				data.map((item)=>{
					if(item.on_sales === 1){
						this.giftList.push(item)
					};
				});
			}
		}
		
	}
</script>
<style lang="scss" scoped>
	@import "./style/gift.scss"
</style>
